package com.lemon.ai.socket.packets.spiritgame.xls2Object.generator;

import java.io.File;
import java.io.FileInputStream;

import com.lemon.ai.socket.packets.spiritgame.model.generator.AbstractGenerator;
import com.lemon.ai.socket.packets.spiritgame.model.utils.FileUtils;
import com.lemon.ai.socket.packets.spiritgame.xls2Object.Constent;

public class XmlGenerator extends AbstractGenerator {
	
	private static FileUtils fu = new FileUtils();
	
	public XmlGenerator() {
		super("baseClassConfig.txt");
		try {
			this.prop.load(new FileInputStream(new File(System
					.getProperty("user.dir")
					+ "\\templetConfig.properties")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void generate(Constent c) {

		if(!c.isClientNeed()) {
			return;
		}
		
		StringBuilder s = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r<root>\r\r");
		s.append("<!-- " + c.explain + " -->\r\r");
//		s.append("\t<names>");
//		for (String n : c.filedNames) {
//			s.append(n + ",");
//		}
//		if(s.toString().endsWith(",")) {
//			s.deleteCharAt(s.length() - 1);
//		}
//		
//		s.append("</names>\r");
		
		String RES_PATH = "res/images/" + c.className.toLowerCase() + "/";
		s.append("\t<classPath>cn.mxz.vo." + c.className + "Vo</classPath>\r");
		s.append("\t<config prefix=\"" + RES_PATH + "\" pictype=\".jpg\" index=\""  + c.filedNames[0] + "\"/>\r");
		
		for (int i = 0; i < c.constent.length; i++) {
			String[] cc = c.constent[i];
			
			String t = "	<item ";
			for (int j = 0; j < c.types.length; j++) {
				String value = cc[j];
				if(!c.marks[j].equals("1")) {//不为1才生成
					t += c.filedNames[j] + "=\"" + value + "\" " ;
				}
			}
			t += "/>"+ "\r";
			s.append(t);
		}
		
		s.append("</root>");
		
		String fname = super.prop.getProperty("dir")
		+ super.prop.getProperty("package").replace(".", "\\")
		 + "\\xml\\" + c.className + "Config"
		+ ".xml";
		
		fu.write(fname, s.toString());
		fu.write(fname + "__" + c.explain, "");
	}

}
